package cn.wolfcode.shop.member.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CookieUtil {


    public static void addCookie(HttpServletResponse response,String cookieName,String cookieValue,int expireSeconds){
        Cookie cookie = new Cookie(cookieName,cookieValue);
        cookie.setDomain("localhost");//作用域，用户请求哪个域名的时候回带上该cookie信息,域名应该是nginx域名或者zuul域名
        cookie.setPath("/");
        cookie.setMaxAge(expireSeconds);
        response.addCookie(cookie);
    }

    public static String getCookie(HttpServletRequest request,String cookieName){
        Cookie[] cookies = request.getCookies();
        if(cookies==null || cookies.length==0){
            //请求中没有cookie的信息,不需要做任何的逻辑
            return null;
        }
        for(Cookie cookie:cookies){
            if(cookie.getName().equals(cookieName)){
                return cookie.getValue();
            }
        }
       return null;
    }
}
